/*
 * Powered By fanrenqij.com
 * Since 2017 - 2017
 */

package com.fanrenqiji.modules.business.messagequeue.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.Lists;

import com.fanrenqiji.entity.MessageQueueEntity;
import com.fanrenqiji.modules.business.messagequeue.vo.MessageQueueVO;
import com.fanrenqiji.modules.business.messagequeue.dao.MessageQueueDao;
import com.fanrenqiji.modules.business.messagequeue.service.MessageQueueService;

import org.dozer.DozerBeanMapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.fanrenqiji.common.persistence.Page;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.Order;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 消息队列业务逻辑实现
 *
 * @author wentao
 */
@Service(value = "messageQueue")
@Transactional(readOnly = true)
public class MessageQueueServiceImpl implements MessageQueueService{

@Autowired
private MessageQueueDao messageQueueDao;

@Autowired
    DozerBeanMapper beanMapper;

/**
 * 插入
 *
 * @param messageQueue
 * 插入的对象
 * @return 插入数据条数
 */
@Transactional
public MessageQueueVO insert(MessageQueueVO messageQueue){
        MessageQueueEntity messageQueueEntity=new MessageQueueEntity();
        beanMapper.map(messageQueue,messageQueueEntity);
        messageQueueDao.save(messageQueueEntity);
        beanMapper.map(messageQueueEntity,messageQueue);
        return messageQueue;
        }


/**
 * 批量插入
 *
 * @param list
 * 数据列表
 * @return 插入数据条数
 */
@Transactional
public void insertList(List<MessageQueueVO>list){
        for(MessageQueueVO messageQueue:list){
        insert(messageQueue);
        }
        }


/**
 * 修改
 *
 * @param messageQueue
 *            修改的数据
 * @return 修改的数据
 */
@Transactional
public MessageQueueVO update(MessageQueueVO messageQueue){
        MessageQueueEntity messageQueueEntity=null;
        if(StringUtils.isNotEmpty(messageQueue.getId())){
        messageQueueEntity=messageQueueDao.findOne(Long.parseLong(messageQueue.getId()));
        }
        if(messageQueueEntity==null){
        messageQueueEntity=new MessageQueueEntity();
        }
        beanMapper.map(messageQueue,messageQueueEntity);
        messageQueueDao.save(messageQueueEntity);
        beanMapper.map(messageQueueEntity,messageQueue);
        return messageQueue;
        }


/**
 * 删除
 *
 * @param id
 *            数据标识
 * @return 删除的数据条数
 */
@Transactional
public void delete(String id){
        messageQueueDao.deleteById(Long.parseLong(id));
        }


/**
 * 查询所有数据
 *
 * @return 数据列表
 */
public List<MessageQueueVO>getAll(){
        List<MessageQueueVO>list=Lists.newArrayList();
        List<MessageQueueEntity>entities=messageQueueDao.findAllList();
        for(MessageQueueEntity messageQueueEntity:entities){
        MessageQueueVO messageQueueVO=new MessageQueueVO();
        beanMapper.map(messageQueueEntity,messageQueueVO);
        list.add(messageQueueVO);
        }
        return list;
        }


/**
 * 通过条件查询数据列表
 *
 * @param messageQueue
 *            查询条件
 * @return 数据列表
 */
public List<MessageQueueVO>getList(MessageQueueVO messageQueue){
        List<MessageQueueVO>list=Lists.newArrayList();
        DetachedCriteria dc=messageQueueDao.createDetachedCriteria();
        dc.add(Restrictions.eq(MessageQueueEntity.DEL_FLAG,MessageQueueEntity.DEL_FLAG_NORMAL));
    /*
    dc.createAlias("messageQueue", "messageQueue"); // as

    dc.add(Restrictions.ne("id", 1L)); // !=

    if (StringUtils.isNotEmpty(messageQueue.getName())) {
        dc.add(Restrictions.like("name", "%" + messageQueue.getName() + "%")); //like
    }
    if (StringUtils.isNotEmpty(messageQueue.getName())) {
        dc.add(Restrictions.eq("name", messageQueue.getName())); //=
    }

	dc.addOrder(Order.asc("office.id")).addOrder(Order.asc("name"));  //排序

    */
        List<MessageQueueEntity>entities=messageQueueDao.find(dc);
        for(MessageQueueEntity messageQueueEntity:entities){
        MessageQueueVO messageQueueVO=new MessageQueueVO();
        beanMapper.map(messageQueueEntity,messageQueueVO);
        list.add(messageQueueVO);
        }
        return list;
        }


/**
 * 通过id查询数据
 *
 * @param id
 *            数据标识
 * @return 数据对象
 */
public MessageQueueVO get(String id){
        MessageQueueVO messageQueue=new MessageQueueVO();
        MessageQueueEntity messageQueueEntity=messageQueueDao.findOne(Long.parseLong(id));
        if(messageQueueEntity==null){
        messageQueueEntity=new MessageQueueEntity();
        }
        beanMapper.map(messageQueueEntity,messageQueue);
        return messageQueue;
        }


/**
 * 根据条件分页查询
 * @param page
 * @param messageQueue
 * @return
 */
public Page<MessageQueueVO>findMessageQueueByPage(Page<MessageQueueVO>page,MessageQueueVO messageQueue){
        Page<MessageQueueEntity>pageEntity=new Page<MessageQueueEntity>(page.getPageNo(),page.getPageSize());
        DetachedCriteria dc=messageQueueDao.createDetachedCriteria();
        dc.add(Restrictions.eq(MessageQueueEntity.DEL_FLAG,MessageQueueEntity.DEL_FLAG_NORMAL));
    /*
         if (StringUtils.isNotEmpty(messageQueue.getName())) {
            dc.add(Restrictions.like("name", "%" + messageQueue.getName() + "%")); //like
        }

    dc.createAlias("messageQueue", "messageQueue"); // as

    dc.add(Restrictions.ne("id", 1L)); // !=


    if (StringUtils.isNotEmpty(messageQueue.getName())) {
        dc.add(Restrictions.eq("name", messageQueue.getName())); //=
    }

    if (!StringUtils.isNotEmpty(page.getOrderBy())) { //排序
	    dc.addOrder(Order.asc("name"))
		  .addOrder(Order.desc("id"));
	}
    */
        if(!StringUtils.isNotEmpty(page.getOrderBy())){
            dc.addOrder(Order.desc("id"));
        }
        pageEntity=messageQueueDao.find(pageEntity,dc);
        beanMapper.map(pageEntity,page);
        List<MessageQueueVO>list=Lists.newArrayList();
        for(MessageQueueEntity messageQueueEntity:pageEntity.getList()){
            MessageQueueVO messageQueueVO=new MessageQueueVO();
            beanMapper.map(messageQueueEntity,messageQueueVO);
            list.add(messageQueueVO);
        }
        page.setList(list);
        return page;
        }


 }
